const app = getApp();
Page({

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const token = wx.getStorageSync('token')
        // console.log(options)
        // 将navigator传递的值赋值给给各自的id（点击时获取）
        this.setData({
            historyId: this.options.historyId,
            mannersId: this.options.mannersId,
            poemId: this.options.poemId,
            flag: this.options.flag,
            likeflag: app.globalData.likeflag,
            index0: app.globalData.index0,
            collectflag: app.globalData.collectflag,
            index1: app.globalData.index1,
        })
        //请求收藏列表
        wx.request({
            url: `${app.globalData.url}/collect`,
            method: "GET",
            dataType: "json",
            header: {
                'content-type': 'text/json',
                'Authorization': token
            },
            success: res => {
                // console.log(res.data)
                //遍历请求到的数据
                for (var i = 0; i < res.data.length; i++) {
                    //历史模块显示
                    if (typeof (res.data[i]["historyId"]) != "undefined") {
                        if (res.data[i]["historyId"] == this.data.historyId) {

                            const historyContent = res.data[i]["historyContent"];
                            // console.log(historyContent)
                            const historyContentArr = historyContent.split('/r/n');
                            app.globalData.collectflag = true,
                                app.globalData.index1 = 1
                            this.setData({
                                likeflag: app.globalData.likeflag,
                                index: app.globalData.index0,
                                historyContent: historyContentArr,
                                collectflag: app.globalData.collectflag,
                                index1: app.globalData.index1,
                                logined: false,
                                id:res.data[i].historyId
                            })
                            this.isLike(this.data.id, this.data.flag);
                            this.isCollect(this.data.id, this.data.flag);
                        }
                    }
                    //礼仪模块显示
                    if (typeof (res.data[i]["mannersId"]) != "undefined") {
                        if (res.data[i]["mannersId"] == this.data.mannersId) {
                            const mannersContent = res.data[i]["mannersContent"];
                            // console.log(mannersContent)
                            const mannersContentArr = mannersContent.split('/r/n');
                            const mannersImg = res.data[i]["mannersImg"]
                            app.globalData.collectflag = true,
                                app.globalData.index1 = 1
                            this.setData({
                                likeflag: app.globalData.likeflag,
                                index: app.globalData.index0,
                                mannersContent: mannersContentArr,
                                mannersImg: "https://api.hiehie.top:6001/images/" + mannersImg,
                                collectflag: app.globalData.collectflag,
                                index1: app.globalData.index1,
                                logined: true,
                                id:res.data[i].mannersId
                            })
                            this.isLike(this.data.id, this.data.flag);
                            this.isCollect(this.data.id, this.data.flag);
                        }
                    }
                    //诗句模块显示
                    if (typeof (res.data[i]["poemId"]) != "undefined") {
                        if (res.data[i]["poemId"] == this.data.poemId) {
                            const poemContent = res.data[i]["poemContent"];
                            app.globalData.collectflag = true,
                                app.globalData.index1 = 1
                            this.setData({
                                likeflag: app.globalData.likeflag,
                                index: app.globalData.index0,
                                poemContent: poemContent,
                                poemTitle: res.data[i].poemTitle,
                                poemContent: poemContent,
                                poet: res.data[i].poet,
                                poemDynasty: res.data[i].poemDynasty,
                                collectflag: app.globalData.collectflag,
                                index1: app.globalData.index1,
                                logined: false,
                                id:res.data[i].poemId
                            })
                            this.isLike(this.data.id, this.data.flag);
                            this.isCollect(this.data.id, this.data.flag);
                        }
                    }
                }


            },
            fail: err => {
                console.log(err);
            }
        })
    },
    // 判断是否在点赞列表里面的函数
    isLike: function (id, flag) {
        const token = wx.getStorageSync('token');
        wx.request({
            url: `${app.globalData.url}/good/id=${id}&flag=${flag}`,
            method: 'GET',
            dataType: 'json',
            header: {
                'Authorization': token
            },
            success: res => {
                if (res.data == 'Not in table') {
                    app.globalData.likeflag = false,
                        app.globalData.index0 = 0;
                    this.setData({
                        likeflag: app.globalData.likeflag,
                        index0: app.globalData.index0
                    })
                } else {
                    app.globalData.likeflag = true,
                        app.globalData.index0 = 1
                    this.setData({
                        likeflag: app.globalData.likeflag,
                        index0: app.globalData.index0
                    })
                }
            },
            fail: err => {
                // console.log(err)
                app.globalData.likeflag = false
                this.setData({
                    likeflag: app.globalData.likeflag
                })
            }
        })
    },
    // 点赞函数
    like: function (id, flag) {
        const token = wx.getStorageSync('token');
        wx.request({
            url: `${app.globalData.url}/good?id=${id}&flag=${flag}`,
            method: 'POST',
            dataType: 'json',
            header: {
                'Authorization': token
            },
            data: {
                id: id,
                flag: flag
            },
            //请求成功setData
            success: res => {
                // console.log('post praise success!');
                app.globalData.likeflag = true,
                    app.globalData.index0 = 1
                this.setData({
                    likeflag: app.globalData.likeflag,
                    index0: app.globalData.index0
                })
            },
            fail: err => {
                console.log(err)
            }

        })
    },
    // 取消点赞函数
    dislike: function (id, flag) {
        const token = wx.getStorageSync('token');
        wx.request({
            url: `${app.globalData.url}/good/id=${id}&flag=${flag}`,
            method: 'DELETE',
            dataType: 'json',
            header: {
                'Authorization': token
            },
            success: res => {
                // console.log("delete praise sucess")
                app.globalData.likeflag = false,
                    app.globalData.index0 = 0
                this.setData({
                    likeflag: app.globalData.likeflag,
                    index: app.globalData.index0
                })
            },
            fail: err => {
                console.log(err)
            }
        })
    },
    //点击点赞图片切换
    open: function () {
        let flag = this.data.flag;
        if (app.globalData.index0 == 0) {
            if (flag == 0) {
                let id = this.data.historyId;
                // 取消点赞函数
                this.like(id, flag)
                // console.log(id, flag)
            } else if (flag == 1) {
                let id = this.data.mannersId;
                // 取消点赞函数
                this.like(id, flag)
                // console.log(id, flag)
            } else if (flag == 2) {
                let id = this.data.poemId
                // 取消点赞函数
                this.like(id, flag)
                // console.log(id, flag)
            }
        } else {
            if (flag == 0) {
                let id = this.data.historyId;
                // 取消点赞函数
                this.dislike(id, flag)
                // console.log(id, flag)
            } else if (flag == 1) {
                let id = this.data.mannersId;
                // 取消点赞函数
                this.dislike(id, flag)
                // console.log(id, flag)
            } else if (flag == 2) {
                let id = this.data.poemId
                // 取消点赞函数
                this.dislike(id, flag)
                // console.log(id, flag)
            }
        }
    },

    // 判断是否在收藏列表里面的函数
    isCollect: function (id, flag) {
        const token = wx.getStorageSync('token');
        wx.request({
            url: `${app.globalData.url}/collect/id=${id}&flag=${flag}`,
            method: 'GET',
            dataType: 'json',
            header: {
                'Authorization': token
            },
            success: res => {
                // console.log(res);
                if (res.data == 'Not in table') {
                    app.globalData.collectflag = false,
                        app.globalData.index1 = 0
                    this.setData({
                        collectflag: app.globalData.collectflag,
                        index1: app.globalData.index1
                    })
                } else {
                    app.globalData.collectflag = true,
                        app.globalData.index1 = 1
                    this.setData({
                        collectflag: app.globalData.collectflag,
                        index1: app.globalData.index1
                    })
                }
            },
            fail: err => {
                // console.log(err)
                app.globalData.collectflag = false,
                    app.globalData.index1 = 0
                this.setData({
                    collectflag: app.globalData.collectflag,
                    index1: app.globalData.index1
                })
            }
        })
    },
    // 收藏函数
    collect: function (id, flag) {
        const token = wx.getStorageSync('token');
        wx.request({
            url: `${app.globalData.url}/collect?id=${id}&flag=${flag}`,
            method: 'POST',
            dataType: 'json',
            header: {
                'Authorization': token
            },
            data: {
                id: id,
                flag: flag
            },
            //请求成功setData
            success: res => {
                // console.log(res);
                // console.log('post success!');
                app.globalData.collectflag = true,
                    app.globalData.index1 = 1
                this.setData({
                    collectflag: app.globalData.collectflag,
                    index1: app.globalData.index1
                })
            },
            fail: err => {
                console.log(err)
            }

        })
    },
    // 取消收藏函数
    discollect: function (id, flag) {
        const token = wx.getStorageSync('token');
        wx.request({
            url: `${app.globalData.url}/collect/id=${id}&flag=${flag}`,
            method: 'DELETE',
            dataType: 'json',
            header: {
                'Authorization': token
            },
            success: async res => {
                // console.log('Delete collect sucess');
                app.globalData.collectflag = false,
                    app.globalData.index1 = 0
                this.setData({
                    collectflag: app.globalData.collectflag,
                    index1: app.globalData.index1
                })

            },
            fail: err => {
                console.log(err)
            }
        })
    },
    //点击收藏图片切换
    open1: function (res) {
        let flag = this.data.flag;
        if (app.globalData.index1 == 0) {
            if (flag == 0) {
                let id = this.data.historyId;
                // 取消点赞函数
                this.collect(id, flag)
                // console.log(id, flag)
            } else if (flag == 1) {
                let id = this.data.mannersId;
                // 取消点赞函数
                this.collect(id, flag)
                // console.log(id, flag)
            } else if (flag == 2) {
                let id = this.data.poemId
                // 取消点赞函数
                this.collect(id, flag)
                // console.log(id, flag)
            }
        } else {
            if (flag == 0) {
                let id = this.data.historyId;
                // 取消点赞函数
                this.discollect(id, flag)
                // console.log(id, flag)
            } else if (flag == 1) {
                let id = this.data.mannersId;
                // 取消点赞函数
                this.discollect(id, flag)
                // console.log(id, flag)
            } else if (flag == 2) {
                let id = this.data.poemId
                // 取消点赞函数
                this.discollect(id, flag)
                // console.log(id, flag)
            }
        }
    },
})